<?php

	$default_custom = array("STB","HAG","SJS","KSS","HDC");
	
    function get_css_class($value){
    	if($value > 0){
    		$class = "up";
        }else {
        	$class="down";
        }
        return $class;
    }
    // temporary. will update later
    $current_stock_code = $this->stock_code;
    $today = mysql_result(mysql_query("SELECT max(`date`) FROM `index_growth`"),0,0);
    $previous_year = date("Y-m-d",strtotime('-1 year',strtotime($today)));
    //$previous_year = "2010-1-20";
    //$num = date("N",($nam_truoc = strtotime ('-1 year' , strtotime ($dayy))));

    
	$a = mysql_query("SELECT `close`,`open` FROM `index_growth` where `ticker` = 'VNINDEX' AND `date` = (SELECT max(`date`) FROM `index_growth`)");
	$vnindex = mysql_result($a,0,0); 
	$op1 = mysql_result($a,0,1);
	$per1 = round((($vnindex - $op1) * 100)/$op1,2);
	                                          
	$b = mysql_query("SELECT `close`,`open` FROM `index_growth` where `ticker` = 'HNXINDEX' AND `date` = (SELECT max(`date`) FROM `index_growth`)");
	$hnxindex = mysql_result($b,0,0);
	$op2 = mysql_result($b,0,1);
	$per2 = round((($hnxindex - $op2)*100)/$op2,2); 
	
	// obtain corellation 
	$sql = "SELECT data from cal_corelation where stock_code='{$current_stock_code}'";
	$res = mysql_query($sql);
	$data = unserialize(mysql_result($res, 0,0));
	arsort($data);
	$header = array_keys($data);
	
	
	// get data
	$num = count($data);
		
	for($i = 1; $i < 4; $i++){
		$highest_correlation[] = $header[$i];
		$lowest_correlation[] = $header[$num-$i];
	}
	
	// DM INDEX		
	$sql = "SELECT `data` FROM `index_port` order by `date` DESC LIMIT 0,1";
	$res = mysql_query($sql);
	$dm_index = unserialize(mysql_result($res,0,0));
	$ret_values = array();
	$port = array();
	for($i = 0 ; $i < count($dm_index);$i++){				
		$result_ret = array();
		$sql = "SELECT `date`, `ret` from `stock_date` where `ticker`='{$dm_index[$i][0]}' and `date` BETWEEN '{$previous_year}' AND '{$today}'";
		$res = mysql_query($sql);
		while($r = mysql_fetch_object($res)){
			$result_ret[$r->date] = $r->ret; 
			
		}
		//$ret_values[$dm_index[$i][0]] = $result_ret;
		// index same as $retvalue
		$ret_values[$i] = $result_ret;
	}
	
	$j = 0;
	
	foreach($ret_values[0] as $key=>$value){
		if(count($port)==0){
			$port[] = 1;
			continue;
		}
		$j++;
		$total = $value*$dm_index[0][3] +1;
		
		for($i = 1; $i < 5; $i++){
			$total+=$ret_values[$i][$key]*$dm_index[$i][3];
		}			
						
		$total = $port[count($port)-1]*$total;		
		$port[] = $total;
	}	
	
	$ret_port = array();
	$ret_port[] = 0;
	$n  = count($port);
	for($i = 1; $i < $n; $i++){
		$ret_port[] = $port[$i]/$port[$i-1]-1;
	}
	
    $rr = stdvep($ret_port)*sqrt(250);    
    $profit = $port[count($port)-1]-1;
    
    $dem_VHCao = mysql_query("SELECT COUNT(`von_thi_truong`) FROM `cal_risk` WHERE `von_thi_truong` > 10000");
    $dem1 = mysql_result($dem_VHCao, 0, 0);
    $dem_VHTb = mysql_query("SELECT COUNT(`von_thi_truong`) FROM `cal_risk` WHERE `von_thi_truong` BETWEEN 2000 AND 10000");
    $dem2 = mysql_result($dem_VHTb, 0, 0);
    $dem_VHThap = mysql_query("SELECT COUNT(`von_thi_truong`) FROM `cal_risk` WHERE `von_thi_truong` BETWEEN 200 AND 2000");
    $dem3 = mysql_result($dem_VHThap, 0, 0);
    $dem_VNIndex = mysql_query("SELECT COUNT(`id`) FROM `company` WHERE `stock_exchange` = 'HOSE'");
    $dem4 = mysql_result($dem_VNIndex, 0, 0);
    $dem_HNXIndex = mysql_query("SELECT COUNT(`id`) FROM `company` WHERE `stock_exchange` = 'HASTC'");
    $dem5 = mysql_result($dem_HNXIndex, 0, 0);
 ?>
                <div class="art-content-layout">
                    <div class="art-content-layout-row">
                        <div class="art-layout-cell art-content">
                                <div class="art-post-body" >
                                <ul class="art-menu-2">
                                    Ngày: <?php echo $today ?> | 
                                    VNINDEX <span style="margin: 0px 15px 0px 15px; "> <?php echo $vnindex; ?></span><span style="color: <?php if($per1>=0) echo "green"; else "red"; ?>; "><?php echo $per1."%" ?></span> |   
                                    HNXINDEX <span style="margin: 0px 15px 0px 15px; "><?php echo $hnxindex; ?></span><span style="color: <?php if($per1>=0) echo "green"; else "red"; ?>;"><?php echo $per2."%" ?></span> |                                      
                                </ul>
                            		<div class="cleared"></div>   
                                </div>
                            <div class="art-post">
                                <div class="art-post-body">
                            <div class="art-post-inner art-article">
                                <div class="art-postcontent">
                                    <br />
                                    <?php
                                        $i = 0; 
                                        if ($i%2){
                                            $class = 'row1';
                                        } 
                                        else {
                                            $class = 'row2';
                                        }
                                        $i += 1;
                                    ?>
                                    <table  class="art-article" border="0" cellspacing="0" cellpadding="0" width="800">
                                          
                                          <tr  bgcolor="#A3CCDC" style="font-style: inherit;">
                                            <th colspan="3">Phân tích rủi ro</th>
                                            <td >Hệ số rủi ro</td>
                                            <td>Xếp hạng rủi ro</td>
                                            <td>Số ngày TK</td>
                                            <td  colspan="2">Đồ thị HSRR</td>
                                            <td >CP tương quan cao</td>
                                            <td>CP tương quan thấp</td>
                                          </tr>
                                          <tr style="font-size:10px;">
                                            <td bgcolor="#C7B6C9" rowspan="3">&nbsp;</td>
                                            <td>Mã CP</td>
                                            <td >Giá(1000)<br />/+-</td>
                                            <td >Giá trị rủi ro T+4 hiệu chỉnh cho</td>
                                            <td >Tỷ lệ CP ít rủi ro hơn CP <?php echo $current_stock_code;?></td>
                                            <td >Số ngày tối ưu để mua/bán 5000cp</td>
                                            <td colspan="2" rowspan="3"><img style="margin:0;padding-top: 60px;" width="160px" src="<?php echo $this->baseUrl() ?>/index/graph7<?php echo $current_stock_code?"/id/".$current_stock_code."/":""; ?>" /></td>
                                            <td >Biến động giống <?php echo $current_stock_code;?>(không nên đầu tư cùng nhằm hạn)</td>
                                            <td >Biến động ngược <?php echo $current_stock_code;?>(có thể xem xét đầu tư)</td>  
                                          </tr>
                                          <tr bgcolor="#CBCAB4">
                                            <td rowspan="2">
                                            	<!--  <input id="mack" name="mack" type="text" value="FPT" style="width: 30px; height:20px" />-->
                                                <form name="frmMack" method="post" action="">
                                            	<select id="mack" name="mack" onchange="window.location='<?php echo $this->baseUrl()?>/index/index/id/'+this.value">
                                            		<?php foreach ($this->companies as $company):?>
                                            		<option <?php echo $company->getCode()==$this->stock_code?'selected="selected"':"";?> value="<?php echo $company->getCode();?>">
                                            			<?php echo $company->getCode();?></option>
                                            		<?php endforeach;?>
                                            	</select>
                                                </form>
                                            </td>
                                            <?php
                                                $sql= mysql_query("SELECT cr.`gia`, cr.`thay_doi`, cr.`hsrr`,cr.`quantile_1`,cr.`so_ngay_tk` FROM `stock_date`st,`cal_risk` cr WHERE st.`ticker`='{$current_stock_code}' AND st.`date`='{$today}' AND st.`id` = cr.`stock_date_id`");                                          
                                                $gia = mysql_result($sql,0,0);
                                                $td = mysql_result($sql,0,1);
                                                $hsrr1=mysql_result($sql,0,2);
                                                $xhrr1 = mysql_result($sql,0,3);
                                                $sntk1= mysql_result($sql,0,4);
                                            ?>
                                            <td><?php echo $gia;?></td>
                                            <td rowspan="2"><?php echo round(($hsrr1*100),2)."%" ;?></td>
                                            <td rowspan="2"><?php echo round(($xhrr1*100),2)."%" ;?></td>
                                            <td rowspan="2"><?php echo round($sntk1,2);?></td>
                                            
                                            <td rowspan="2"><?php echo implode($highest_correlation, ", "); ?></td>
                                            <td rowspan="2"><?php echo implode($lowest_correlation, ", "); ?></td>
                                          </tr>
                                          <tr>
                                            <td><?php echo $td;?></td>
                                          </tr>
                                          <tr>
                                            <td colspan="10"></td>
                                          </tr>
                                          <?php $sql = mysql_query("SELECT `ticker`,cr.`hsrr`,st.`close`,cr.`quantile_1`,cr.`so_ngay_tk` FROM `stock_date` st,`cal_risk` cr  WHERE cr.`stock_date_id` = st.`id` ORDER by cr.`hsrr` ASC LIMIT 0,10");
                                                $ticker = array();
                                                $price = array();
                                                $hsrr = array();
                                                $xhrr = array();
                                                $sktk = array();
                                                
                                                $rr_result = array();
                                                for($i=0; $i <10 ; $i ++)
                                                {
                                                    $ticker[$i] = mysql_result($sql,$i,0);
                                                    $price[$i] = mysql_result($sql,$i,2);
                                                    $hsrr[$i] = mysql_result($sql,$i,1);
                                                    $xhrr[$i] = mysql_result($sql,$i,3);
                                                    $sntk[$i] = mysql_result($sql,$i,4);
                                                    $sql2  = "SELECT `date`, `ret` from `stock_date` where `ticker`='".$ticker[$i]."' and `date` BETWEEN '{$previous_year}' AND '{$today}'";                                                    
                                                    $res = mysql_query($sql2);
                                                    $temp = array();
                                                	while($r = mysql_fetch_object($res)){
														$temp[$r->date] = $r->ret; 														
													}
													$rr_result[] = $temp;													
                                                }
                                                
                                                $rr_port = array();
                                                $rr_port[] = 1;
                                                $j = 0;
                                                                                                
                                                foreach($rr_result[0] as $key=>$value){
																									
													$j++;
													$total = $value;
													
													
													for($i = 1; $i < 10; $i++){
														$total+=$rr_result[$i][$key];
													}
							
													$total = $rr_port[count($rr_port)-1]*(1+$total/10);
													
													$rr_port[] = $total;
												}
												$rr_ret_port = array();
												$rr_ret_port[] = 0;
												$n  = count($rr_port);
												for($i = 1; $i < $n; $i++){
													$rr_ret_port[] = $rr_port[$i]/$rr_port[$i-1]-1;
												}	
																								
												
												$rr_dmsmall = stdvep($rr_ret_port)*sqrt(250);    
    											$profit_dmsmall = $rr_port[count($rr_port)-1]-1;
    											
    											
    											//d($rr_dmsmall,$profit_dmsmall);die;
                                                
                                                
                                          ?>
                                          <tr>
                                            <th colspan="6">Danh mục Cổ phiếu hạn chế rủi ro T+4 - Khả năng thua lỗ thấp nhất</th>
                                            <td colspan="2" rowspan="3">
                                                <img style="margin:0;padding-top: 100px;" width="160px" src="<?php echo $this->baseUrl() ?>/index/graph2<?php echo $current_stock_code?"/id/".$current_stock_code."/":""; ?>" />
                                            </td>
                                            <td colspan="2" rowspan="3">
                                            Diễn biến giá cổ phiếu
                                            <img src="<?php echo $this->baseUrl() ?>/index/graph1/<?php echo $current_stock_code?"id/".$current_stock_code."/":""; ?>" />
                                            </td>
                                          </tr>
                                          <tr bgcolor="#BFD9A6" >
                                            <td colspan="2">&nbsp;</td>
                                            <td>Giá</td>
                                            <td>HSRR</td>
                                            <td>Xếp hạng RR</td>
                                            <td>Số ngày TK</td>
                                          </tr>
                                          <tr class="<?php echo $class; ?>">
                                            <td><?php for($i = 1; $i <= 10; $i++) echo $i."<br>";  ?> </td>
                                            <td><?php for($i = 0; $i < 10; $i++)  echo $ticker[$i]."<br>"; ?> </td>
                                            <td><?php for($i = 0; $i < 10; $i++)  echo $price[$i]."<br>"; ?></td>
                                            <td><?php for($i = 0; $i < 10; $i++)  echo round(($hsrr[$i]*100),2)."%"."<br>"; ?></td>
                                            <td><?php for($i = 0; $i < 10; $i++)  echo round($xhrr[$i]*100,2)."%"."<br>"; ?></td>
                                            <td><?php for($i = 0; $i < 10; $i++)  echo round($sntk[$i],2)."<br>"; ?></td>
                                          </tr>
                                    </table>            
                                    <br />
                                    <table class="art-article" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <th colspan="4">Danh mục VNINDEX - Đầu tư theo chỉ số không phải một sự lựa chọn tồi</th>
                                            <td rowspan="7">&nbsp;</td>
                                            <th colspan="4">Danh mục Tự chọn - Hãy thử đánh giá diễn biến danh mục của bạn</th>
                                            
                                            <td colspan="4" rowspan="7"><img id="graph3" src="<?php echo $this->baseUrl() ?>/index/graph3" /></td>
                                        </tr>
                                        <tr>
                                            <td  bgcolor="#CBCAB4">&nbsp;</td>
                                            <td  bgcolor="#CBCAB4">CP</td>
                                            <td  bgcolor="#CBCAB4">Cơ cấu</td>
                                            <td >Rủi ro</td>
                                            <td  bgcolor="#CBCAB4">&nbsp;</td>
                                            <td  bgcolor="#CBCAB4">CP</td>
                                            <td  bgcolor="#CBCAB4">Cơ cấu</td>
                                            <td >Rủi ro</td>
                                        </tr>
                                        <form action="" id="custom_port_form" name="custom_port_form" > 
                                        <?php
                                        	$i = 1;                                         
                                        	foreach($dm_index as $stock):
                                        ?>
                                        <tr >                                    
                                            <td bgcolor="#CBCAB4"  ><?php echo $i;?></td>
                                            <td><?php echo $stock[0];?></td>
                                            <td><?php echo round($stock[3]*100,1)."%";?></td>
                                            <?php
                                            	if($i==1){
                                            		$class = get_css_class($rr);                                            			
                                            		echo "<td class=\"{$class} \" >".round($rr*100,2)."</span>%</td>";
                                            	} 
                                            	else if($i == 2){
                                            		echo "<td ><b>Lợi nhuận</b></td>";
                                            	}
                                            	else if($i==3){
                                            		$class = get_css_class($profit);
                                                    echo "<td class='{$class}'>".round($profit*100,2)."%</td>";
                                            	}
                                            	else{
                                            		echo "<td>&nbsp;</td>";
                                            	}
                                            ?>
                                            
                                            <td bgcolor="#CBCAB4"><?php echo $i;?></td>
                                            
                                            <td><input type="text" name="custom_stock_<?php echo $i ;?>" id="custom_stock_<?php echo $i ;?>" value="<?php echo $default_custom[$i-1]; ?>"/></td>
                                            <td><input type="text" name="custom_per_<?php echo $i ;?>" id="custom_per_<?php echo $i ;?>" value="20" />%</td>
                                            
                                            	<?php
                                            	if($i==1){
                                            		$class = get_css_class($rr);                                            			
                                            		//echo "<td class=\"{$class} \" ><span class='custom_rr'>".round($rr*100,2)."</span>%</td>";
                                            		echo "<td class=\"{$class} \" ><span class='custom_rr'>(#) </span>%</td>";
                                            	} 
                                            	else if($i == 2){
                                            		echo "<td><b>Lợi nhuận</b></td>";
                                            	}
                                            	else if($i==3){
                                            		$class = get_css_class($profit);
                                            		//echo "<td class='{$class}'><span class='custom_profit'>".round($profit*100,2)."</span>%</td>";
                                            		echo "<td class='{$class}'><span class='custom_profit'>(#) </span>%</td>";
                                            	}
                                            	else if($i==4){
                                            		echo "<td><input type=\"submit\" name=\"submit\" value=\"Cal\"/></td>";
                                            	}
                                            	else{
                                            		echo "<td>&nbsp;</td>";
                                            	}
                                            	$i++;
                                            ?>
                                            
                                        </tr>
                                        <?php endforeach;?>
                                        </form>    
                                    </table>
                                    
                                    <br />
                                        <?php 
                                            $danh_muc = array(11);
                                            $danh_muc[0] = "CP ".$current_stock_code;//'FPT'; //insert csdl sau
                                            $ck = $current_stock_code;
                                            $danh_muc[1] = 'DM tự chọn';
                                            $danh_muc[2] = 'DM VNINDEX';
                                            $danh_muc[3] = 'DM rủi ro thấp';
                                            $danh_muc[4] = 'DM VH cao';
                                            $danh_muc[5] = 'DM VB tb';
                                            $danh_muc[6] = 'DM VH thấp';
                                            $danh_muc[7] = 'VNINDEX ';
                                            $danh_muc[8] = 'HNXINDEX';
                                            $danh_muc[9] = 'SP500';
                                            $danh_muc[10] = 'SH comp';
                                            
                                            $dayy = date('Y-m-d', strtotime($today));
                                            //$day_y = date('m/d/Y', strtotime($today));
                                            $c = mysql_query("SELECT cr.`ret_12m` FROM `cal_risk`cr, `stock_date`st WHERE st.`ticker`='$ck' AND st.`date` = '$today' AND cr.`stock_date_id` = st.`id`");
                                            
                                            
                                            $d = mysql_query("SELECT `dm_large_mc`,`dm_mid_mc`,`dm_small_mc` FROM `cal_port` WHERE `date` = '$dayy' ");
                                            
                                            $vn_ht = mysql_result(mysql_query("SELECT `close` FROM `index_growth` WHERE `date`= '$dayy' AND `ticker`='VNINDEX' "),0,0);
                                            $hn_ht = mysql_result(mysql_query("SELECT `close` FROM `index_growth` WHERE `date`= '$dayy' AND `ticker`='HNXINDEX' "),0,0);
                                            $sql = "SELECT `close`,`date` FROM `index_growth` WHERE `ticker`='SP500' order by `date` DESC LIMIT 0,1 ";                                            
                                            $sp_ht = mysql_result(mysql_query($sql),0,0);                                            
                                            $sh_ht = mysql_result(mysql_query("SELECT `close` FROM `index_growth` WHERE `ticker`='HANGSENG' order by `date` DESC LIMIT 0,1"),0,0);
                                            
                                            $num = date("N",($nam_truoc = strtotime ('-1 year' , strtotime ($dayy))));
                                            $ngay = date('Y-m-d',$nam_truoc);
                                            if($num == 6)
                                                $ngay = strtotime ( '-1 day' ,$nam_truoc);
                                            else if($num == 7)
                                                $ngay = strtotime('+1 day', $nam_truoc);
                                          
                                                    
                                            $res = mysql_query("SELECT `close` FROM `index_growth` WHERE `date`= '$ngay' AND `ticker`='VNINDEX'");
                                            $res1 = mysql_query("SELECT `close` FROM `index_growth` WHERE `date`= '$ngay' AND `ticker`='HNXINDEX'");
                                            $res2 = mysql_query("SELECT `close` FROM `index_growth` WHERE `date`= '$ngay' AND `ticker`='SP500'");
                                            $res3 = mysql_query("SELECT `close` FROM `index_growth` WHERE `date`= '$ngay' AND `ticker`='HANGSENG'");
                                            $vn_namtruoc = mysql_result($res,0,0);
                                            $hn_namtruoc = mysql_result($res1,0,0);
                                            $sp_namtruoc = mysql_result($res2,0,0);
                                            $sh_namtruoc = mysql_result($res3,0,0);
                                            
                                            $loi_nh = mysql_result($c,0,0);
                                            $num5 = round((((mysql_result($d,0,0))/100-1)*100),2);
                                            $num6 =  round((((mysql_result($d,0,1))/100-1)*100),2);
                                            $num7 = round((((mysql_result($d,0,2))/100-1)*100),2);
                                            
                                            $num1 = round(($loi_nh*100),2);
                                            $num3 = round(($profit *100),2);
                                            $num8 = round((($vn_ht/$vn_namtruoc-1)*100),2);
                                            $num9 = round((($hn_ht/$hn_namtruoc-1)*100),2);
                                            $num10 = round((($sp_ht/$sp_namtruoc-1)*100),2);
                                            $num11 = round((($sh_ht/$sh_namtruoc-1)*100),2);
                                            
                                            //------
                                            $s = mysql_query("SELECT cr.`vol_12m` FROM `stock_date` st,`cal_risk` cr  WHERE cr.`stock_date_id` = st.`id` AND st.`ticker`='$ck'");
                                            $dlc_ = (mysql_result($s,0,0)*sqrt(250)*100); 
                                            $dlc_1 = round($dlc_,2);

                                            $g = mysql_query("SELECT COUNT(`dm_large_mc_ret`) FROM `cal_port`"); 
                                            $h = mysql_query("SELECT `dm_large_mc_ret`,`dm_mid_mc_ret`,`dm_small_mc_ret`,`dm_index_ret` FROM `cal_port`");
                                            $g1 = mysql_query("SELECT COUNT(`close`) FROM `index_growth` WHERE `ticker`='HNXINDEX' AND `date` >= '$previous_year'");
                                            $g2 = mysql_query("SELECT COUNT(`close`) FROM `index_growth` WHERE `ticker`='SP500' AND `date` >= '$previous_year'");
                                            $g3 = mysql_query("SELECT COUNT(`close`) FROM `index_growth` WHERE `ticker`='HANGSENG' AND `date` >= '$previous_year'");
                                            
                                            $k = mysql_query("SELECT `close` FROM `index_growth` WHERE `ticker`='HNXINDEX' AND `date` >= '$previous_year'");
                                            $k2 = mysql_query("SELECT `close` FROM `index_growth` WHERE `ticker`='SP500' AND `date` >= '$previous_year'");
                                            $k3 = mysql_query("SELECT `close` FROM `index_growth` WHERE `ticker`='HANGSENG' AND `date` >= '$previous_year'");
                                            
                                            
                                            $count = mysql_result($g,0,0);
                                            $count1 = mysql_result($g1,0,0);
                                            $count2 = mysql_result($g2,0,0);
                                            $count3 = mysql_result($g3,0,0);
                                            
                                            $g_arr = array($count-1);
                                            $h_arr = array($count-1);
                                            $i_arr = array($count-1);
                                            $j_arr = array($count-1);
                                            $k_arr = array($count1-1);
                                            $k2_arr = array($count2-1);
                                            $k3_arr = array($count3-1);
                                            
                                            for($i = 0; $i < $count ;$i++)
                                            {
                                                  $g_arr[$i] = mysql_result($h,$i,0)*100;
                                                  $h_arr[$i] = mysql_result($h,$i,1)*100;    
                                                  $i_arr[$i] = mysql_result($h,$i,2)*100;
                                                  $j_arr[$i] = mysql_result($h,$i,3)*100;
                                                  
                                            }
                                            $k_arr[0] = 0;
                                            for ($j = 1; $j < $count1; $j++ )
                                            {
                                                $a1 = mysql_result($k,$j-1,0);
                                                $b1 = mysql_result($k,$j,0);
                                                
                                                $k_arr[$j] = (($b1-$a1)/$a1)*100;
                                                
                                            }
                                            $k2_arr[0] = 0;
                                            for ($k = 1; $k < $count2; $k++ )
                                            {
                                                $a2 = mysql_result($k2,$k-1,0);
                                                $b2 = mysql_result($k2,$k,0);
                                                
                                                $k2_arr[$k] = (($b2-$a2)/$a2)*100;
                                            }
                                            
                                            $k3_arr[0] = 0;
                                            for ($l = 1; $l < $count3; $l++ )
                                            {
                                                $a3 = mysql_result($k3,$l-1,0);
                                                $b3 = mysql_result($k3,$l,0);
                                                
                                                $k3_arr[$l] = (($b3-$a3)/$a3)*100;
                                            }
                                            
                                            $dlc_3 = round($rr*100,2);
                                            $dlc_5 = round((stdvep($g_arr)* sqrt(250)),2);
                                            $dlc_6 = round((stdvep($h_arr)* sqrt(250)),2);
                                            $dlc_7 = round((stdvep($i_arr)* sqrt(250)),2);
                                            $dlc_8 = round((stdvep($j_arr)* sqrt(250)),2);
                                            $dlc_9 = round((stdvep($k_arr)* sqrt(250)),2);
                                            $dlc_10 = round((stdvep($k2_arr)* sqrt(250)),2);
                                            $dlc_11 = round((stdvep($k3_arr)* sqrt(250)),2);
                                            
                                            $ln1 = round(($num1/100 - 0.08)/($dlc_1/100),1);
                                            $ln3 = round(($num3/100 - 0.08)/($dlc_3/100),1);
                                            $ln5 = round(($num5/100 - 0.08)/($dlc_5/100),1);
                                            $ln6 = round(($num6/100 - 0.08)/($dlc_6/100),1);
                                            $ln7 = round(($num7/100 - 0.08)/($dlc_7/100),1);
                                            $ln8 = round(($num8/100 - 0.08)/($dlc_8/100),1);
                                            $ln9 = round(($num9/100 - 0.08)/($dlc_9/100),1);
                                            $ln10 = round(($num10/100 - 0.08)/($dlc_10/100),1);
                                            $ln11 = round(($num11/100 - 0.08)/($dlc_11/100),1);
                                        ?>
                                        <?php
                                            $a = mysql_query("SELECT `stock_code` FROM `cal_risk` ORDER BY `hsrr` LIMIT 0,10" );
                                            $arr = array(10);
                                            $brr = 0;
                                            
                                            for($i = 0; $i <10 ; $i++)
                                            {
                                                $arr[$i] = mysql_result($a,$i,0);
                                                $b = mysql_query("SELECT `ret` FROM `stock_date` WHERE `ticker`='$arr[$i]' AND `date`='$today' ");
                                                $brr += mysql_result($b,0,0);
                                            }
                                            $port_o = 0.922229; //port 20-1-2011
                                            $port_n = round((($port_o *(1+($brr)/10) - 1)*100),1);
                                            
                                            $ret_ruiro = mysql_query("SELECT `ret` FROM `stock_date` WHERE `ticker`='AAM' AND `date` >= '$previous_year'");
                                            $date_ruiro = mysql_query("SELECT COUNT(`ret`) FROM `stock_date` WHERE `ticker`='AAM' AND `date` >= '$previous_year'");
                                            $n = mysql_result($date_ruiro, 0, 0);
                                            $gia_cophieu = array();
                                            $gia = array();
                                            $gia[0] = 1;
                                            $cophieu_value = array();
                                            for($i = 0; $i < $n; $i++){
                                                $gia_cophieu[] = mysql_result($ret_ruiro, $i, 0);
                                                $gia[$i+1] = $gia[$i] * (1 + $gia_cophieu[$i]);
                                                $cophieu_value[$i] = $gia[$i] * 100;
                                       		}
                                            //d($gia_cophieu);die;
                                        ?>
                                    
                                    <table class="art-article" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <th colspan="4">Danh mục theo vốn hóa - BLUECHIP và PENNY: Ai đang dẫn đầu?</th>
                                            <td rowspan="3">&nbsp;</td>
                                            <td colspan="4" rowspan="3"><img src="<?php echo $this->baseUrl() ?>/index/graph4" /></td>
                                            <td rowspan="3"><img src="<?php echo $this->baseUrl() ?>/index/graph5" /></td>
                                        </tr>
                                        <tr bgcolor="#8080C0">
                                            <td>&nbsp;</td>
                                            <td>&nbsp; </td>
                                            <td>Rủi ro</td>
                                            <td>Lợi nhuận</td>
                                        </tr>
                                        <?php 
                                            $dm = array("DM VH cao: 15cp, VH >10000 tỷ",
                                                        "DM VH TB: 41cp,VH từ 2000-10000 tỷ",
                                                        "DM VH thấp: 259cp, VH từ 200-2000T"
                                            );
                                            $dm_rr = array("$dlc_5","$dlc_6","$dlc_7");
                                            $dm_ln = array("$ln5","$ln6","$ln7");
                                        ?>
                                        <tr>
                                            <td bgcolor="#6B9C63"><?php for ($i=1; $i<=3; $i++)
                                                echo $i."<br>"; 
                                            ?></td>
                                            
                                            <td bgcolor="#E9E9E9" ><?php for ($i=0; $i<3; $i++)
                                                echo $dm[$i]."<br>";
                                            ?></td>
                                            <td bgcolor="#E9E9E9"><?php for ($i=0; $i<3; $i++)
                                                echo $dm_rr[$i]."%"."<br>"; 
                                            ?></td>
                                            <td bgcolor="#E9E9E9"><?php for ($i=0; $i<3; $i++)
                                                echo $dm_ln[$i]."%"."<br>"; 
                                            ?></td>
                                        </tr>
                                    </table>
                                    <br />
                                    <table class="art-article" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <th colspan="6">Thống kê Danh mục - Lợi nhuận có tỷ lệ với rủi ro?</th>
                                            <td rowspan="3" colspan="3">&nbsp;</td>
                                            <td rowspan="3" bgcolor="#ACF29F"><img src="<?php echo $this->baseUrl();?>/index/graph6" /></td>
                                        </tr>
                                        <tr bgcolor="#8080C0">
                                            <td colspan="2">&nbsp;</td>
                                            <td>SL cổ phiếu</td>
                                            <td>Lợi nhuận</td>
                                            <td>Độ lệch chuẩn</td>
                                            <td>Sharpe</td>                                                    
                                        </tr>
                                        <tr class="<?php echo $class; ?>">                                                    
                                            <td bgcolor="#6B9C63"><?php for($i =1 ; $i<=11 ; $i++) echo $i."<br>"; ?></td>
                                            <td bgcolor="#E9E9E9"><?php for($i =0 ; $i< 11 ; $i++) echo $danh_muc[$i]."<br>"; ?></td>
                                            <td bgcolor="#E9E9E9"><?php for($i =1 ; $i<=11 ; $i++) 
                                                { 
                                                    if($i==1) echo "1" . "<br>";
                                                    else if($i==2) echo "5" . "<br>";
                                                    else if($i==3) echo "5" . "<br>";
                                                    else if($i==4) echo "10" . "<br>";
                                                    else if($i==5) echo $dem1 . "<br>"; 
                                                    else if($i==6) echo $dem2 . "<br>";
                                                    else if($i==7) echo $dem3 . "<br>";
                                                    else if($i==8) echo $dem4 . "<br>";
                                                    else if($i==9) echo $dem5 . "<br>";
                                                    else if($i==10) echo ""  . "<br>";
                                                    else echo "" . "<br>";
                                                }  
                                                ?>
                                            </td>
                                            <?php
                                                $mack = mysql_query("SELECT `stock_code` FROM `cal_risk` ORDER BY `hsrr` LIMIT 0,10");
                                                $name = array();
                                                for($i=0; $i<10; $i++)
                                                {
                                                    $name[$i] = mysql_result($mack, $i, 0);   
                                                }
                                                $ret = mysql_query("SELECT `ret` FROM `stock_date` WHERE `ticker` = 'HAG' AND `date` >= '$previous_year'");
                                                $count = mysql_query("SELECT Count(`ret`) FROM `stock_date` WHERE `ticker` = 'AAM' AND `date` >= '$previous_year'");
                                                $n = mysql_result($count, 0, 0);
                                                $ret_value = array();
                                                for($j=0; $j<$n; $j++)
                                                {
                                                    $ret_value[$j] = mysql_result($ret, $j, 0);   
                                                }
                                               // d($ret_value);die;                                    
                                            ?>
                                            <form action="" id="custom_port_form" name="custom_port_form" >
                                            <td bgcolor="#E9E9E9"><?php for($i =1 ; $i<=11 ; $i++) 
                                                { 
                                                    if($i==1) echo $num1."%"."<br>";
                                                    else if($i==2) 
                                                    	echo "<span class='custom_profit'>(#) %</span><br/>";
                                                        //echo "<span class='custom_profit'>".round($profit*100,2)."%</span><br/>";
                                                    else if($i==3) echo $num3."%"."<br>";
                                                    else if($i==4) echo round($profit_dmsmall*100,2)."%"."<br>";
                                                    else if($i==5) echo $num5."%"."<br>"; 
                                                    else if($i==6) echo $num6."%"."<br>";
                                                    else if($i==7) echo $num7."%"."<br>";
                                                    else if($i==8) echo $num8."%"."<br>";
                                                    else if($i==9) echo $num9."%"."<br>";
                                                    else if($i==10) echo $num10."%"."<br>";
                                                    else echo $num11."%"."<br>";
                                                }  
                                            ?></td>
                                            </form>
                                            <td bgcolor="#E9E9E9"><?php for($i =1 ; $i<=11 ; $i++) 
                                                { 
                                                    if($i==1) echo $dlc_1."%"."<br>";
                                                    //else if ($i==2) echo "<span class='custom_rr'>".round($rr*100,2)."</span>%" ."<br>";
                                                    else if ($i==2) echo "<span class='custom_rr'>(#) </span>%" ."<br>";
                                                    else if($i==3) echo $dlc_3."%"."<br>";
                                                    else if($i==4) echo round($rr_dmsmall*100,2) ."%<br>";
                                                    else if($i==5) echo $dlc_5."%"."<br>"; 
                                                    else if($i==6) echo $dlc_6."%"."<br>";
                                                    else if($i==7) echo $dlc_7."%"."<br>";
                                                    else if($i==8) echo $dlc_8."%"."<br>";
                                                    else if($i==9) echo $dlc_9."%"."<br>";
                                                    else if($i==10) echo $dlc_10."%"."<br>";
                                                    else echo $dlc_11."%"."<br>";
                                                       
                                                }  
                                            ?>
                                            </td>
                                            <td bgcolor="#E9E9E9">
                                                <?php for($i =1 ; $i<=11 ; $i++) 
                                                { 
                                                    if($i==1) echo $ln1."<br>";
                                                    //else if ($i==2) echo "<span class='custom_sharpe'>".round((($rr*100) - 0.08)/($profit*100), 1)."</span><br>";
                                                    else if ($i==2) echo "<span class='custom_sharpe'>(#)</span><br>";
                                                    else if($i==3) echo $ln3."<br>";
                                                    else if($i==4) echo round(($profit_dmsmall-0.08)/$rr_dmsmall,1) ."<br>";
                                                    else if ($i==5) echo $ln5."<br>"; 
                                                    else if($i==6) echo $ln6."<br>";
                                                    else if($i==7) echo $ln7."<br>";
                                                    else if($i==8) echo $ln8."<br>";
                                                    else if($i==9) echo $ln9."<br>";
                                                    else if($i==10) echo $ln10."<br>";
                                                    else echo $ln11."<br>";
                                                }  
                                                ?>
                                            </td>
                                            
                                        </tr>
                                    </table>
                                </div>
                                <div class="cleared"></div>
                            </div>                            
                            		<div class="cleared"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
 <script>
 	 
	 $("form").submit(function() {
		 var params={};
         var str = '';
		 $("input[type='text']").each(function(){
			 if($(this).attr('disabled'))
				 return;
			 //if(this.value)
			 	params[this.name] = this.value;
                str += '&' + encodeURIComponent(this.name) + '=' + encodeURIComponent(this.value);
		 }); 
		$.ajax({
			url: '<?php echo $this->baseUrl();?>/index/process/',
			data: params,			
			success: function(msg){
				res = $.parseJSON(msg);	
				$(".custom_rr").html(res.ruiro);
				$(".custom_profit").html(res.loinhuan);
				//round((($rr*100) - 0.08)/($profit*100), 1)
				
				var sharpe = Math.round(((res.loinhuan)/100- 0.08)/(res.ruiro/100)*1000)/1000;
				$(".custom_sharpe").html(sharpe);
				
			}
		});
        $('#graph3').attr('src', '<?php echo $this->baseUrl() ?>/index/graph3/?' + str);
		 return false;
	 });

 </script>                
